﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Threading;
using System.Xml;
using System.IO;
namespace Business
{
    public partial class SecurityLog : Form
    {
        public MainForm m_mainform;
        public SecurityLog()
        {
            InitializeComponent();
        }
        public void getjilu()
        {
            lv.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from dengluriji Where dlsj>'{0}' and dlsj<'{1}'", dateTimePicker1.Value.ToShortDateString() + " 00:00:00", dateTimePicker2.Value.ToShortDateString() + " 23:59:59");

            sql = sql + " order by dlsj desc ";
            //保存操作日记
            m_mainform.savecaozuoriji(sql, "查询", "业务系统", "查询登录日志");
            //try
            {
                DataTable dt = mquery.GetTable(sql);

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {


                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["dlqk"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["username"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["dengluIP"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["dlsfcg"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["dlsj"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["dljg"].ToString().Trim());
                        this.lv.Items.Add(lt);
                    }
                }


            }



        }

        private void but_query_Click(object sender, EventArgs e)
        {
            getjilu();
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();
        }
        public void tongji()
        {
            lv2.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@" select username from dengluriji group by username having count(username)>0 ");//用户名
            DataTable dt = mquery.GetTable(sql);
            var name = "";
            var sdate = "";

           
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["username"].ToString().Trim() + ":";
                   
                    sql = string.Format(@"select * from dengluriji where username='{0}' 
                                            and dlsj>'{1}' ",
                                            dt.Rows[i]["username"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_us = name.Split(new char[] { ':' });
            string[] cdate_us = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select dengluIP from dengluriji group by dengluIP having count(dengluIP)>0 ");//事件
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["dengluIP"].ToString().Trim() + ":";
                    
                    sql = string.Format(@"select * from dengluriji where dengluIP='{0}' 
                                            and dlsj>'{1}' ",
                                            dt.Rows[i]["dengluIP"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_ip = name.Split(new char[] { ':' });
            string[] cdate_ip = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select dlqk from dengluriji group by dlqk having count(dlqk)>0 ");//事件
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["dlqk"].ToString().Trim() + ":";
                  
                    sql = string.Format(@"select * from dengluriji where dlqk='{0}' 
                                            and dlsj>'{1}' ",
                                            dt.Rows[i]["dlqk"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }

            string[] cname_dlqk = name.Split(new char[] { ':' });
            string[] cdate_dlqk = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select dljg from dengluriji group by dljg having count(dljg)>0 ");//事件
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["dljg"].ToString().Trim() + ":";
                   
                    sql = string.Format(@"select * from dengluriji where dljg='{0}' 
                                            and dlsj>'{1}' ",
                                            dt.Rows[i]["dljg"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_jg = name.Split(new char[] { ':' });
            string[] cdate_jg = sdate.Split(new char[] { ':' });


            var count = 0;
            if (count < cname_us.Length)
            {
                count = cname_us.Length;
            }
            if (count < cname_ip.Length)
            {
                count = cname_ip.Length;
            }
            if (count < cname_dlqk.Length)
            {
                count = cname_dlqk.Length;
            }
            if (count < cname_jg.Length)
            {
                count = cname_jg.Length;
            }
           

            count = count - 1;
            for (int i = 0; i < count; i++)
            {
                ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                if (i < cname_us.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_us[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_us[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //2
                if (i < cname_ip.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_ip[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_ip[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //3
                if (i < cname_dlqk.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_dlqk[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_dlqk[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //4
                if (i < cname_jg.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_jg[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_jg[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

               



                this.lv2.Items.Add(lt);

            }

        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();

            string path = "";
            path = Application.StartupPath + path + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            excel(path);
        }
        private void excel(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();

                sb.Append("统计时间从" + dateTimePicker1.Value.ToShortDateString() + "至" + DateTime.Now.ToString("yyyy-MM-dd") + "\t");
                sb.Append(Environment.NewLine);
                for (int i = 0; i < lv2.Columns.Count; i++)
                    sb.Append(lv2.Columns[i].Text.ToString() + "\t");
                sb.Append(Environment.NewLine);

                foreach (ListViewItem li in lv2.Items)
                {
                    for (int i = 0; i < lv2.Columns.Count; i++)
                        sb.Append(li.SubItems[i].Text + "\t");
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("成功保存于" + path + "文件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SecurityLog_Load(object sender, EventArgs e)
        {
            //if (!m_mainform.squanxian.Contains("登录日志查询"))
            //{
            //    but_query.Enabled = false;
            //}

            //if (!m_mainform.squanxian.Contains("登录日志统计"))
            //{
            //    skinButton1.Enabled = false;
            //}

            //if (!m_mainform.squanxian.Contains("登录日志导出"))
            //{
            //    skinButton2.Enabled = false;
            //}

        }
    }
}
